package com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper.TGypsumPlanMapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ITGypsumPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.GypsumPlanVo;
import com.xiaoxu.intranetweb.util.StringUtils;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoxu
 * @since 2024-07-04
 */
@Service
public class TGypsumPlanServiceImpl extends ServiceImpl<TGypsumPlanMapper, TGypsumPlan> implements ITGypsumPlanService {
    @Autowired
    private CProductsInfoServiceImpl productsInfoService;
    @Autowired
    private CCompanysServiceImpl companysService;
    @Override
    public SzResult getAllGypsumPlan(Integer page, Integer pageSize,String pdtId, String startTime, String endTime, String plantId) {
        QueryWrapper<TGypsumPlan> gypsumPlanQueryWrapper = new QueryWrapper<>();
        gypsumPlanQueryWrapper.eq(StringUtils.isNotEmpty(pdtId),"PDT_ID",pdtId);
        gypsumPlanQueryWrapper.ge(StringUtils.isNotEmpty(startTime),"PLAN_TIME",startTime);
        gypsumPlanQueryWrapper.le(StringUtils.isNotEmpty(endTime),"PLAN_TIME",endTime);
        gypsumPlanQueryWrapper.ne("DEL_FLAG",1);
        gypsumPlanQueryWrapper.orderByDesc("PLAN_TIME");
        Page<TGypsumPlan> Page = this.page(new Page<>(page, pageSize), gypsumPlanQueryWrapper);
        List<GypsumPlanVo> list = Page.getRecords().stream().map((item)->{
            GypsumPlanVo gypsumPlanVo = new GypsumPlanVo();
            BeanUtils.copyProperties(item,gypsumPlanVo);
            CProductsInfo productsInfo = productsInfoService.getById(gypsumPlanVo.getPdtId());
            if (productsInfo != null){
                gypsumPlanVo.setPdtName(productsInfo.getPdtinfPdt());
            }
            CCompanys company = companysService.getById(gypsumPlanVo.getCoId());
            if (company != null){
                gypsumPlanVo.setCo(company.getCpyCo()+'.'+company.getCpyChico());
            }
            CCompanys supplyCompany = companysService.getById(gypsumPlanVo.getSupplyCoId());
            if (supplyCompany != null){
                gypsumPlanVo.setSupplyCo(supplyCompany.getCpyCo()+'.'+supplyCompany.getCpyChico());
            }
            return gypsumPlanVo;
        }).collect(Collectors.toList());

        Map<String, Object> map = new HashMap<>();
        map.put("list",list);
        map.put("total", Page.getTotal());
        return SzResult.success(map);
    }
    @Override
    public SzResult addGypsumPlan(TGypsumPlan gypsumPlan, SysUser user) {
        if (gypsumPlan.getPlanTime()!=null && StringUtils.isNotEmpty(gypsumPlan.getPdtId()) && StringUtils.isNotEmpty(gypsumPlan.getCoId())){
            QueryWrapper<TGypsumPlan> gypsumPlanQueryWrapper = new QueryWrapper<>();
            gypsumPlanQueryWrapper.eq("PDT_ID",gypsumPlan.getPdtId());
            gypsumPlanQueryWrapper.eq("CO_ID",gypsumPlan.getCoId());
            gypsumPlanQueryWrapper.eq("PLAN_TIME",gypsumPlan.getPlanTime().truncatedTo(ChronoUnit.DAYS));
            gypsumPlanQueryWrapper.ne("DEL_FLAG",1);
            if (this.list(gypsumPlanQueryWrapper).size() >0) {
                return SzResult.error("已有重复计划");
            }
        }
        if(gypsumPlan.getPlanTime()!=null){
            gypsumPlan.setDayWeek(gypsumPlan.getPlanTime().getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINESE));
        }
        gypsumPlan.setCreateBy(user.getTrueName());
        gypsumPlan.setCreateTime(LocalDateTime.now());
        gypsumPlan.setDelFlag(0);
        if (this.save(gypsumPlan)) {
            return SzResult.success("添加成功");
        }
        return SzResult.error("添加失败");
    }
    @Override
    public SzResult updateGypsumPlan(TGypsumPlan gypsumPlan, SysUser user) {
        if (gypsumPlan.getPlanTime()!=null && StringUtils.isNotEmpty(gypsumPlan.getPdtId()) && StringUtils.isNotEmpty(gypsumPlan.getCoId())){
            QueryWrapper<TGypsumPlan> gypsumPlanQueryWrapper = new QueryWrapper<>();
            gypsumPlanQueryWrapper.eq("PDT_ID",gypsumPlan.getPdtId());
            gypsumPlanQueryWrapper.eq("CO_ID",gypsumPlan.getCoId());
            gypsumPlanQueryWrapper.eq("PLAN_TIME",gypsumPlan.getPlanTime());
            gypsumPlanQueryWrapper.ne("DEL_FLAG",1);
            if (this.getOne(gypsumPlanQueryWrapper) != null) {
                return SzResult.error("已有重复计划");
            }
        }
        if(gypsumPlan.getPlanTime()!=null){
            gypsumPlan.setDayWeek(gypsumPlan.getPlanTime().getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINESE));
        }
        if (this.updateById(gypsumPlan)) {
            return SzResult.success("修改成功");
        }
        return SzResult.error("修改失败");
    }



    @Override
    public SzResult delGypsumPlan(String id) {
        TGypsumPlan gypsumPlan = this.getById(id);
        gypsumPlan.setDelFlag(1);
        if (this.updateById(gypsumPlan)) {
            return SzResult.success("删除成功");
        }
        return SzResult.error("删除失败");
    }


}
